package com.fenghongzhang.day010;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.net.URI;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ContentResolver contentResolver;
    private Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //内容解析者的对象
        contentResolver = getContentResolver();
        /**
         *
         * baidu.com
         *
         * http://www.baidu.com
         */
        //com.feng
        uri = Uri.parse("content://com.feng");


    }

    public void query(View view) {
        Cursor query = contentResolver.query(uri, null, null, null, null);

        if(query != null){
            while(query.moveToNext()){
                String id = query.getString(query.getColumnIndex("id"));
                String name = query.getString(query.getColumnIndex("name"));
                Log.i(TAG, "query: "+id+":"+name);
            }

            query.close();
        }

    }

    public void insert(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","小白");
        contentResolver.insert(uri,contentValues);
    }

    public void del(View view) {
        contentResolver.delete(uri,"id= ?",new String[]{"6"});
    }

    public void update(View view) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","小小白");
        contentResolver.update(uri,contentValues,"id= ?",new String[]{"6"});
    }
}
